package com.example.demo111.handler;


import com.example.demo111.entity.Project;
import com.example.demo111.entity.UserInfo;
import com.example.demo111.service.ProjectService;
import com.example.demo111.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;


@Controller
public class UserInfoHandler {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private ProjectService projectService;

    @RequestMapping( "/login")
    public String login(UserInfo userinfo, Model model) {
        Integer a = userInfoService.selectByNameAndPwd(userinfo);
        List<Project> proList = this.projectService.getItem();
        model.addAttribute("pRo", proList);
        if (a == 1) {
            return "index";}
        if (a == 2) {
            UserInfo user = this.userInfoService.search();
            model.addAttribute("auser", user.getName());
            return "admin";
        }
        return "login";

    }
    @RequestMapping( "/zhuce")
    public String login1(UserInfo userInfo) {
        Integer a = this.userInfoService.Userzc(userInfo);
        if(1001==a){
            return "login";
        }
        return "Register";

    }
    @RequestMapping("password")
    public String updatePassword(String name,String pwd,String npwd,String qpwd){
        if (!npwd.equals(qpwd))throw new RuntimeException("新密码和确认密码必须一致");
        if (npwd.equals("")|qpwd.equals(""))throw new RuntimeException("新密码或确认密码不能为空");
        userInfoService.updatePassword(name,pwd,npwd);
        return "redirect:/password";
    }
}
